package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.dto.TTopicDto;
import com.ruoyi.system.domain.po.TTopicExample;
import com.ruoyi.system.domain.po.TTopic;
import com.ruoyi.system.domain.po.TTopicExample;
import com.ruoyi.system.domain.po.TTopic;
import com.ruoyi.system.domain.vo.TTopicVo;
import com.ruoyi.system.mapper.TTopicMapper;
import com.ruoyi.system.service.TTopicService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author nanyang
 */
@Service
public class TTopicServiceImpl implements TTopicService {
    
    @Resource
    private TTopicMapper topicMapper;
    
    
    @Override
    public Integer insertTopic(TTopicDto.Insert topicInsert) {
        // 新增实体数据
        TTopic topic = BeanUtil.copyProperties(topicInsert, TTopic.class);
        topic.setId(IdUtil.getSnowflake().nextId());
        topic.setCreateBy(SecurityUtils.getUsername());
        topic.setCreateTime(new Date());
        return topicMapper.insertSelective(topic);
    }

    @Override
    public Integer updateTopic(TTopicDto.Update topicUpdate) {
        // 修改实体
        TTopic topic = BeanUtil.copyProperties(topicUpdate, TTopic.class);
        topic.setUpdateBy(SecurityUtils.getUsername());
        topic.setUpdateTime(new Date());
        return topicMapper.updateByPrimaryKeySelective(topic);
    }

    @Override
    public Integer deleteTopic(TTopicDto.Delete topicDelete) {
        TTopicExample interfaceExample = new TTopicExample();
        interfaceExample.createCriteria().andIdIn(topicDelete.getIdList());
        TTopic topic = new TTopic();
        topic.setDelFlag((byte) 2);
        return topicMapper.updateByExampleSelective(topic, interfaceExample);
    }

    @Override
    public List<TTopicVo> listTopic(TTopicDto.Query queryTopic) {
        TTopicExample topicExample = new TTopicExample();
        TTopicExample.Criteria criteria = topicExample.createCriteria().andDelFlagEqualTo((byte) 0);
        if (StrUtil.isNotBlank(queryTopic.getTopicName())) {
            criteria.andTopicNameLike("%" + queryTopic.getTopicName() + "%");
        }
        if (StrUtil.isNotBlank(queryTopic.getTopicShortName())) {
            criteria.andTopicShortNameLike("%" + queryTopic.getTopicShortName() + "%");
        }
        if (queryTopic.getType() != null) {
            criteria.andTypeEqualTo(queryTopic.getType());
        }
        if (StrUtil.isNotBlank(queryTopic.getCategory())) {
            criteria.andCategoryLike("%" + queryTopic.getCategory() + "%");
        }
        if (StrUtil.isNotBlank(queryTopic.getLeading())) {
            criteria.andLeadingLike("%" + queryTopic.getLeading() + "%");
        }
        if (StrUtil.isNotBlank(queryTopic.getPerson())) {
            criteria.andPersonLike("%" + queryTopic.getPerson() + "%");
        }
        if (queryTopic.getStatus() != null) {
            criteria.andStatusEqualTo(queryTopic.getStatus());
        }
        topicExample.setOrderByClause("create_time desc");
        List<TTopic> topicList = topicMapper.selectByExample(topicExample);
        return topicList.stream().map(e -> BeanUtil.copyProperties(e, TTopicVo.class)).collect(Collectors.toList());
    }
}
